package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OperateBean;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.PingYin;
import org.swan.bean.MessageBean;

import java.util.List;

public class AddOperation implements ServerInterface {

    private static Logger log = LogManager.getLogger(AddOperation.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "添加成功", List.class);
        try {
            OperateBean operate = JsonUtils.objectToPojo(req.getData(), OperateBean.class);
            if (StringUtils.isEmpty(operate.getOpName())) {
                msg.setCode(Constant.MESSAGE_INT_FAIL);
                msg.setDescription("菜单名称不能为空");
                return msg;
            }
            if (operate.getOpFlag() == null) {
                msg.setCode(Constant.MESSAGE_INT_FAIL);
                msg.setDescription("菜单类型不能为空");
                return msg;
            }
            if (StringUtils.isEmpty(operate.getSymbol())) {
                operate.setSymbol(PingYin.getInstance().getSelling(operate.getOpName(), false, ""));
            }
            operate.setApp(ApplicationConfig.getAppCustomerAppid());
            ServiceFactory.getOperationService().addOperation(operate);
        } catch (Exception e) {
            log.error("新增菜单出错：" + e, e);
            msg.setCode(Constant.MESSAGE_INT_FAIL);
            msg.setDescription(e.getMessage());
        }
        return msg;
    }
}
